package com.zyn.es.basic;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutionException;

import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.script.Script;
import org.elasticsearch.script.ScriptService.ScriptType;

/**
 * 更新
 * 
 * https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-docs-update.html
 * 
 * @author tongyufu
 *
 */
public class UpdateAPI extends EsClient {

    public static void main(String[] args) throws Exception {
        UpdateAPI up = new UpdateAPI();
        //up.updateRequest();
        //up.updateByScript();
        //up.prepareUpdate();
        up.upsert();
    }

    /**
     * 存在更新，不存在则新增
     */
    public void upsert() throws Exception {
        super.initClient();
        log.info("================================");
        Map<String, Object> json = new HashMap<String, Object>();
        IndexRequest indexRequest = new IndexRequest(INDEX, TYPE, "2");
        json.put("title", "index request");
        indexRequest.source(json);

        UpdateRequest updateRequest = new UpdateRequest(INDEX, TYPE, "2");
        json.put("title", "update request");
        updateRequest.doc(json).upsert(indexRequest);
        UpdateResponse response = client.update(updateRequest).get();

        super.printResponse(response);
        client.close();
        log.info("================================");
    }

    public void updateRequest() throws InterruptedException, ExecutionException {
        super.initClient();
        log.info("================================");
        UpdateRequest request = new UpdateRequest();
        request.index(INDEX);
        request.type(TYPE);
        request.id("1");

        Map<String, Object> json = new HashMap<String, Object>();
        json.put("title", "up New version of Elasticsearch released!");
        json.put("content", "up Version 5.0 released today!");
        json.put("priority", 11);
        json.put("tags", new String[] { "up", "announce", "elasticsearch", "release" });
        request.doc(json);
        UpdateResponse response = client.update(request).get();

        super.printResponse(response);
        client.close();
        log.info("================================");
    }

    public void updateByScript() {
        super.initClient();
        log.info("================================");
        String scriptCtx = "ctx._source.title=\"script title\"";
        Script script = new Script(scriptCtx, ScriptType.INLINE, null, null);
        UpdateResponse response = client.prepareUpdate(INDEX, TYPE, "1").setScript(script).get();

        super.printResponse(response);
        client.close();
        log.info("================================");
    }

    public void prepareUpdate() {
        super.initClient();
        log.info("================================");
        Map<String, Object> json = new HashMap<String, Object>();
        json.put("title", "prepareUpdate title");
        UpdateResponse response = client.prepareUpdate(INDEX, TYPE, "1").setDoc(json).get();

        super.printResponse(response);
        client.close();
        log.info("================================");
    }
}
